#include <unistd.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define  DELAY	(2)

typedef struct msg {
	int num;
	sem_t sem;
}MSG;

static MSG data;

static void *func_post(void *arg)
{
	while(1) {
	    sleep(DELAY*2);

	    data.num--;
	    printf("[post] data.num = %d\n", data.num);

	    if(data.num%7==0) {
	    	printf("sem post\n");
	    	sem_post(&data.sem);
	    }
	}

}

static void *func_wait(void *arg)
{
	while(1) {
	    sleep(DELAY);

	    printf("[wait]data.num = %d\n", data.num);
	    data.num = 13;
	    sem_wait(&data.sem);
	}

}

int main(int argc, char const *argv[])
{
	pthread_t pth1, pth2;
	int ret;

	data.num = 18;

	ret = sem_init(&data.sem, 0, 0);
	if(ret==-1) {
		printf("sem init err\n");
		exit(1);
	}

	ret = pthread_create(&pth1, NULL, func_post, NULL);
	if(ret!=0) {
		printf("pthread post create err\n");
		exit(1);		
	}

	ret = pthread_create(&pth2, NULL, func_wait, NULL);
	if(ret!=0) {
		printf("pthread wait create err\n");
		exit(1);
	}

	while(1) {
	    ;
	}


	return 0;
}



